package wh.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import wh.bos.domain.base.FixedArea;
import wh.bos.service.base.FixedAreaService;
import wh.bos.web.action.BaseAction;
import wh.crm.domain.Customer;


@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixdeAreaAction extends BaseAction<FixedArea>{

	//注入Service
	@Autowired
	private FixedAreaService fixedAreaService;
	
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}
	//关联客户             fixedArea_associationCustomerToFixedArea
	@Action(value="fixedArea_associationCustomerToFixedArea",results=
		{@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCustomerToFixedArea(){
		String customerIdStr = StringUtils.join(customerIds,",");
		WebClient.create("http://localhost:9003/services/customerService/associationcustomertofixedarea?customerIdStr="
		+customerIdStr+"&fixedAreaId="+model.getId()).put(null); 
		
		return SUCCESS;
	}   
	
	
	
	//查询未关联用户
	@Action(value="fixedArea_findNoAssociationCustomers",results=
		{@Result(name="success",type="json")})
	public String findNoAssociationCustomers(){
		//使用WebClient调用WebService接口
		Collection<? extends Customer> collection = WebClient.create
				("http://localhost:9003/services/customerService/noassociationcustomers")
		.accept(MediaType.APPLICATION_JSON)
		.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
		
	}
	//查询已关联用户
	@Action(value="fixedArea_findAssociationFixedAreaCustomers",results=
		{@Result(name="success",type="json")})
	public String findAssociationFixedAreaCustomers(){
		Collection<? extends Customer> collection = WebClient.create
				("http://localhost:9003/services/customerService/associationfixedareacustomers/"+model.getId())    
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
		
	}
	
	
	
	//添加或是修改定区
	@Action(value="fixedArea_save",results={@Result(name="success",type="redirect",
			location="./pages/base/fixed_area.html")})
	public String fixdeArea_save(){
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	
	@Action(value="fixed_area_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//构造条件查询对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				if (StringUtils.isNoneBlank(model.getCompany())) {
					Predicate p2 =cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Pageable pageable = new PageRequest(page-1, rows);
		//调用业务层
		Page<FixedArea> pageDate = fixedAreaService.findPageData(specification,pageable);
		
		pushPageDateToValueStack(pageDate);
		
		return SUCCESS;
	}

}
